package com.youi.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youi.dao.LeaderprofitdetailVoDao;
import com.youi.entity.Leaderprofit;
import com.youi.entity.vo.LeaderprofitdetailVo;
import com.youi.service.LeaderprofitdetailVoService;
import com.youi.util.DateUtil;
import com.youi.util.FileUploadUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.SmsUtil;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

@Service
public class LeaderprofitdetailVoServiceImpl implements LeaderprofitdetailVoService {

	@Resource
	private LeaderprofitdetailVoDao leaderprofitdetailVoDao;
	
	@Override
	public Integer getLeaderprofitdetailVoCount(String start, String end, String orderNo,
			String phone, String pms) {
		// TODO Auto-generated method stub
		return leaderprofitdetailVoDao.getLeaderprofitdetailVoCount(start, end, orderNo, phone, pms);
	}

	@Override
	public List<LeaderprofitdetailVo> getLeaderprofitdetailVoList(String start, String end, String orderNo,
			String phone, String pms, Integer pageIndex, Integer pageNum) {
		// TODO Auto-generated method stub
		return leaderprofitdetailVoDao.getLeaderprofitdetailVoList(start, end, orderNo, phone, pms, pageIndex, pageNum);
	}

	@Override
	public String toExcelLeaderprofitdetailVo(HttpServletRequest request,
			List<LeaderprofitdetailVo> leaderprofitdetailVoList) {
		// TODO Auto-generated method stub		
		try {
			String fileName="leaderprofitdetail"+new Date().getTime()+""+SmsUtil.getRandom(3)+".xls";//文件名				
			String fileUrl=FileUploadUtil.getUploadUrl(request, "filedownload"+File.separator+fileName);//生成路径
			WritableWorkbook workbook = Workbook.createWorkbook(new File(fileUrl));
			WritableSheet sheet1=workbook.createSheet("Sheet1", 0);
			WritableFont font=new WritableFont(WritableFont.createFont("微软雅黑"),10);
			WritableCellFormat format=new WritableCellFormat();
			format.setAlignment(Alignment.CENTRE);
			format.setVerticalAlignment(VerticalAlignment.CENTRE);
			Label label=new Label(0, 0, "佳选官ID", format);
			sheet1.addCell(label);
			label=new Label(1, 0, "佳选官名称", format);
			sheet1.addCell(label);
			label=new Label(2, 0, "佳选官手机号", format);
			sheet1.addCell(label);
			label=new Label(3, 0, "单号", format);
			sheet1.addCell(label);
			label=new Label(4, 0, "发生时间", format);
			sheet1.addCell(label);
			label=new Label(5, 0, "金额", format);
			sheet1.addCell(label);
			label=new Label(6, 0, "类型", format);
			sheet1.addCell(label);
			LeaderprofitdetailVo leaderprofitdetailVo=null;
			for (int i = 0,j =1; i < leaderprofitdetailVoList.size(); i++,j++) {
				leaderprofitdetailVo=leaderprofitdetailVoList.get(i);
				ObjectUtil.ToChangeNullToEmpty(leaderprofitdetailVo);
				label=new Label(0, j, String.valueOf(leaderprofitdetailVo.getLeaderId()), format);
				sheet1.addCell(label);
				label=new Label(1, j, leaderprofitdetailVo.getLeaderName(), format);
				sheet1.addCell(label);
				label=new Label(2, j, leaderprofitdetailVo.getPhone(), format);
				sheet1.addCell(label);
				label=new Label(3, j, leaderprofitdetailVo.getOrderNo(), format);
				sheet1.addCell(label);
				label=new Label(4, j, DateUtil.simpdfyMdHms.format(leaderprofitdetailVo.getGmtCreated()), format);
				sheet1.addCell(label);
				label=new Label(5, j, String.valueOf(leaderprofitdetailVo.getAmount()), format);
				sheet1.addCell(label);
				label=new Label(6, j, ObjectUtil.getLeaderprofitType(leaderprofitdetailVo.getType()), format);
				sheet1.addCell(label);
			}
			workbook.write();
			workbook.close();
			fileUrl="filedownload/"+fileName;
			return fileUrl;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return null;
	}

	@Override
	public Leaderprofit getLeaderprofitdetailSum() {
		// TODO Auto-generated method stub
		return leaderprofitdetailVoDao.getLeaderprofitdetailSum();
	}

	@Override
	public Double getLeaderprofitdetailByType(Integer type, String start, String end, String orderNo, String phone,
			String pms) {
		// TODO Auto-generated method stub
		return leaderprofitdetailVoDao.getLeaderprofitdetailByType(type, start, end, orderNo, phone, pms);
	}

}
